package com.example.combwx.mvp.model;

import com.example.combwx.mvp.base.model.BaseEntity;
import com.example.combwx.mvp.base.model.BaseModel;
import com.example.combwx.mvp.contract.RegisterContract;
import com.example.combwx.mvp.model.entity.RegisterEntity;
import com.example.combwx.mvp.model.entity.ResponseUserEntity;
import com.example.combwx.network.Api;
import com.example.combwx.network.ChangeFunction;
import com.example.combwx.network.HttpFactory;
import com.example.combwx.network.HttpType;

import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.RequestBody;

public class RegisterModel extends BaseModel implements RegisterContract.IRegisterModel {

    @Inject
    protected RegisterModel(){}

    @Override
    public Observable<BaseEntity> register(Map<String, Object> map) {
        int code = (int) map.get("code");
        map.remove("code");
        if (code == HttpCode.LOGINCODE){
            RequestBody body = createBody(map);
            Observable<ResponseUserEntity> ob = HttpFactory.getInstance()
                    .factory(HttpType.TOKENSIGNTYPE).getRetrofit()
                    .create(Api.class)
                    .requestLogin(body);
            return ob.map(new ChangeFunction<ResponseUserEntity>());
        }else if (code == HttpCode.REGISTCODE){
            RequestBody body = createBody(map);
            Observable<ResponseUserEntity> ob = HttpFactory.getInstance()
                    .factory(HttpType.TOKENSIGNTYPE)
                    .getRetrofit()
                    .create(Api.class)
                    .requestRegister(body);
            return ob.map(new ChangeFunction<ResponseUserEntity>());
        }else if (code == HttpCode.UPDATEUSERCODE){
            RequestBody body = createBody(map);
            Observable<ResponseUserEntity> ob = HttpFactory.getInstance()
                    .factory(HttpType.TOKENSIGNTYPE)
                    .getRetrofit()
                    .create(Api.class)
                    .requestUpdateUser(body);
            return ob.map(new ChangeFunction<ResponseUserEntity>());
        }
        return null;
    }
}
